import org.apache.commons.beanutils.PropertyUtils

import com.thientan.model.NhanVien
import com.thientan.model.core.AuditingInfo

class BootStrap {
	def grailsApplication
	def init = { servletContext ->
		grailsApplication.getArtefacts("Domain").each { org.codehaus.groovy.grails.commons.GrailsDomainClass gc ->
			gc.metaClass.beforeInsert = {
				def insertedDate = new Date()
				gc.persistantProperties.findAll { it.type == AuditingInfo }.each { prop ->
					PropertyUtils.setProperty(owner.delegate, "${prop.name}",
							new AuditingInfo(ngayTao: insertedDate, nvTao: NhanVien.read(1), ngayCapNhat: insertedDate, nvCapNhat: NhanVien.read(1)))
				}
			}

			gc.metaClass.beforeUpdate = {
				gc.persistantProperties.findAll { it.type == AuditingInfo }.each { prop ->
					PropertyUtils.setProperty(owner.delegate, "${prop.name}.ngayCapNhat", new Date())
					PropertyUtils.setProperty(owner.delegate, "${prop.name}.nvCapNhat", NhanVien.read(1))
				}
			}
		}
	}
	def destroy = {
	}
}
